import {deleteById, getListForPage, getRecordById} from "../../services/quotation/quotationRecord";
import {message} from 'antd';
import Constants from '../../utils/Constants'
const R = require('ramda');

export default {
    namespace: 'quotationRecord',
    state: {
        page: {},
        list: [],
        dataSource: {},
        quotation: {},
        customer: {},
        quotationAmount:{}, 
        localFee:[],
        trailerFee:[], 
        declareFee:[]
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState( state, { payload } ) {
            return { ...state, ...payload };
        }
    },
    effects: {
        /**
         * 分页查询报价记录
         *
         * @param call
         * @param put
         */* getListForPage({payload}, {call, put}) {
            const res = yield call(getListForPage, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 根据ID查询报价记录详情
         *
         * @param id
         */* getRecordById({payload}, {call, put}) {
            const res = yield call(getRecordById, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: res.data.content,
                        quotation: res.data.content.quotation,
                        customer: res.data.content.customer,
                        localFee: res.data.content.localFee,
                        trailerFee: res.data.content.trailerFee,
                        declareFee: res.data.content.declareFee,
                        quotationAmount: res.data.content.quotationAmount
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 删除报价记录
         *
         * @param payload
         * @param call
         * @param put
         */
            *deleteById({ payload }, { call }) {
            const res = yield call(deleteById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("报价记录删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};
